package sisi.ports;

import java.util.List;

import javax.persistence.EntityManager;


/**
 * PortController.java - Controller class for the <i>port database table</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 20/11/2013
 * 
 */

public class PortController {

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Port[] getPort(String cerca) {
		List<Port> ports = getPortList(cerca);
		return ports.toArray(new Port[0]);
	}

	@SuppressWarnings("unchecked")
	public List<Port> getPortList(String cerca) {

		String cWhere = "";
		if (cerca != null && !cerca.isEmpty()) {
			cWhere = " WHERE TRIM( BOTH FROM UPPER(c.portname) ) LIKE :cerca OR TRIM( BOTH FROM UPPER(c.country) ) LIKE :cerca ";
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
					.createQuery("select c from Port as c " + cWhere
							+ " order by c.portname");
			
			if (cerca != null && !cerca.isEmpty()) {
				q.setParameter("cerca", '%'+cerca.trim().toUpperCase()+'%' );
			}
			
			List<Port> ar1 = q.getResultList();
			return ar1;
		} finally {
			em.close();
		}
	}

	public List<String> getCountryList() {

		String cWhere = "";

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
					.createQuery("select DISTINCT c.country from Port as c " + cWhere
							+ " order by c.country");
			
			List<String> ar1 = q.getResultList();
			return ar1;
		} finally {
			em.close();
		}
	}
	
	
	public Port getPortxId(int id) {

		EntityManager em = getEntityManager();
		try {
			Port portx = em.find(Port.class, id);
			return portx;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Port addPort(Port port) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(port);
			em.getTransaction().commit();
			port = refreshPort(port);
		} finally {
			em.close();
			return port;
		}
	}

	@SuppressWarnings("finally")
	public boolean updatePort(Port port) {

		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Port portx = em.find(Port.class, port.getId());
			portx.setId(port.getId());
			portx.setCity(port.getCity());
			portx.setContactname1(port.getContactname1());
			portx.setContactname2(port.getContactname2());
			portx.setContactname3(port.getContactname3());
			portx.setCountry(port.getCountry());
			portx.setDtup(port.getDtup());
			portx.setEmail(port.getEmail());
			portx.setEw(port.getEw());
			portx.setFaxcn1(port.getFaxcn1());
			portx.setFaxcn2(port.getFaxcn2());
			portx.setFaxcn3(port.getFaxcn3());
			portx.setLatdegrees(port.getLatdegrees());
			portx.setLondegrees(port.getLondegrees());
			portx.setNote(port.getNote());
			portx.setPortname(port.getPortname());
			portx.setRegion(port.getRegion());
			portx.setSn(port.getSn());
			portx.setTelcn1(port.getTelcn1());
			portx.setTelcn2(port.getTelcn2());
			portx.setTelcn3(port.getTelcn3());
			portx.setWebsite(port.getWebsite());			
			em.getTransaction().commit();
			em.flush();

		} finally {
			em.close();
			return false;
		}
	}

	@SuppressWarnings("finally")
	public Port refreshPort(Port port) {

		Port portx = port;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			portx = em.find(Port.class, port.getId());
			em.flush();
			em.refresh(portx);
		} finally {
			em.close();
			return portx;
		}
	}

	@SuppressWarnings("finally")
	public boolean removePort(Port port) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Port portx = em.find(Port.class, port.getId());
			em.remove(portx);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
	}

}
